package cn.kgc.ebookmanagement.controller;

import cn.kgc.ebookmanagement.pojo.Ebookentry;
import cn.kgc.ebookmanagement.pojo.ResultEntity;
import cn.kgc.ebookmanagement.service.EbookentryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class EntryController {
    @Resource
    EbookentryService ebookentryService;
/*查询分页*/
    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Ebookentry>> selectEntry(
            @RequestParam(value = "categoryId",required = false,defaultValue = "0") Integer categoryId,
                @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "2") Integer pageSize
    ){
PageInfo<Ebookentry> pageInfo=ebookentryService.slelect(pageNum, pageSize, categoryId);
return  ResultEntity.successWithoutData(pageInfo);
    }
/*前端请求post  ,参数事json必须添加@resquestBody*/
    @RequestMapping("/saveEntry")
    public  ResultEntity<Integer> saveEntry(@RequestBody Ebookentry entry){
        Integer add=0;
        if (entry.getId()==null){
            /*添加*/
             add = ebookentryService.add(entry);
        }else {
            //修改
             add = ebookentryService.update(entry);
        }

        return  ResultEntity.successWithoutData(add);
    }

/*根据id查询*/
    @RequestMapping("/getEntryById")
    public  ResultEntity<Ebookentry>  getByid(Integer id){
        Ebookentry ebookentry = ebookentryService.seleteByid(id);
        return  ResultEntity.successWithoutData(ebookentry);
    }

    /*根据id删除*/
    @RequestMapping("deleEntryByid")
    public  ResultEntity<Integer> deleEntryByid (Integer id){
        Integer integer=ebookentryService.delete(id);
        return  ResultEntity.successWithoutData(integer);
    }
}

